package com.jxc.summer.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.jxc.summer.service.CustomerService;
import com.jxc.summer.util.ReturnT;
import com.jxc.summer.util.UUIDUtil;

@Controller
@RequestMapping("/customerinfo")
public class CustomerController {

	@Autowired
	private CustomerService customerService;
	
	@RequestMapping
	public String index(Model model) {
		return "customer/customer.index";
	}
	
	@RequestMapping("/pageList")
	@ResponseBody
	public Map<String, Object> pageList(@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam(required = false, defaultValue = "0") int start,  
			@RequestParam(required = false, defaultValue = "10") int length,@RequestParam String customerName,  
			@RequestParam String contacts,@RequestParam String telephone) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_customername", customerName);
		params.put("tc_contacts", contacts);
		params.put("tc_telephone", telephone);
		params.put("tc_companycode", companyCode);
		params.put("offset", start);
		params.put("pagesize", length);
		Map<String, Object> result = customerService.pageList(params);
		return result;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public ReturnT<String> addCustomer(@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam String customername,  
			@RequestParam String contacts,@RequestParam String telephone,@RequestParam String address,@RequestParam String warehouse,
			@RequestParam String remark) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_customerid",UUIDUtil.get32UUID());
		params.put("tc_customername", customername);
		params.put("tc_contacts", contacts);
		params.put("tc_telephone", telephone);
		params.put("tc_address", address);
		params.put("tc_warehouse", warehouse);
		params.put("tc_remark", remark);
		params.put("tc_companycode", companyCode);
		//当前商户下客户名不能重复
		if(!customerService.validateCustomersname(params)){
			return new ReturnT<String>(500, "新增客户失败！客户公司已存在。");
		}
		int result = customerService.addCustomer(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "新增客户失败！");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public ReturnT<String> updateCustomer(@RequestParam(required = true) String id,
			@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam String customername,  
			@RequestParam String contacts,@RequestParam String telephone,@RequestParam String address,@RequestParam String warehouse,
			@RequestParam String remark){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_customerid", id);
		params.put("tc_customername", customername);
		params.put("tc_contacts", contacts);
		params.put("tc_telephone", telephone);
		params.put("tc_address", address);
		params.put("tc_warehouse", warehouse);
		params.put("tc_remark", remark);
		params.put("tc_companycode", companyCode);
		if(!customerService.validateUpdateCustomersname(params)){
			return new ReturnT<String>(500, "新增客户失败！客户公司已存在。");
		}
		int result = customerService.updateCustomer(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "更新客户失败！");
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public ReturnT<String> deleteAccount(@RequestParam(required = true) String id){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_customerid", id);
		int result = customerService.deleteCustomer(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "删除账户失败！");
		}
	}
	
	@RequestMapping("/getinfo")
	@ResponseBody
	public Map<String,Object> getinfo(@RequestParam(required = true) String id){
		return customerService.getinfo(id);
		
	}
	
}
